import {createRouter, createWebHashHistory, createWebHistory, RouteRecordRaw} from "vue-router";
import {ElMessage} from "element-plus";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/LoginView/LoginView.vue"),
  },
  {
    path: "/",       // 这个是根路由
    name: "home",
    redirect: "/handHygiene/handHygieneRecord",
    component: () => import("@/views/HomeView/HomeView.vue"),
    children: [
      {
        path: "/handHygiene",
        name: "handHygiene",
        meta: {
          isShow: true,
          title: '手卫生',
          icon: 'icon iconfont icon-shouweisheng'
        },
        children: [
          {
            path: "/handHygiene/handHygieneRecord",
            name: "handHygieneRecord",
            meta: {
              isShow: true,
              title: '手卫生记录'
            },
            component: () => import("@/views/handHygiene/handHygieneRecord/handHygieneRecord.vue"),
          },
          {
            path: "/handHygiene/complianceRecord",
            name: "complianceRecord",
            meta: {
              isShow: true,
              title: '依从记录'
            },
            component: () => import("@/views/handHygiene/complianceRecord/complianceRecord.vue"),
          },
          {
            path: "/scene/scene",
            name: "scene",
            meta: {
              isShow: true,
              title: 'three演示'
            },
            component: () => import("@/views/handHygiene/scene/scene.vue"),
          },
        ]
      },
      {
        path: "/systemConfig",
        name: "systemConfig",
        meta: {
          isShow: true,
          title: '系统配置',
          icon: 'icon iconfont icon-a-zu4891'
        },
        children: [
          {
            path: "/systemConfig/region",
            name: "region",
            meta: {
              isShow: true,
              title: '区域管理'
            },
            component: () => import("@/views/systemConfig/region/region.vue"),
          },
          {
            path: "/systemConfig/badge",
            name: "badge",
            meta: {
              isShow: true,
              title: '胸牌管理'
            },
            component: () => import("@/views/systemConfig/badge/badge.vue"),
          },
          {
            path: "/systemConfig/device",
            name: "device",
            meta: {
              isShow: true,
              title: '设备管理'
            },
            component: () => import("@/views/systemConfig/device/device.vue"),
          },
          {
            path: "/systemConfig/member",
            name: "member",
            meta: {
              isShow: true,
              title: '成员管理'
            },
            component: () => import("@/views/systemConfig/member/member.vue"),
          },
        ]
      },
    ]
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

const whiteList: Array<string> = [
  "/login",
  "/loading"
];

router.beforeEach(async (to, from, next) => {
  const hasToken = localStorage.getItem('token');
  if (whiteList.includes(to.path)) {
    next();
  }else if (hasToken) {
    next();
  } else {
    ElMessage.error("登录过期")
    next({ path: "/login", replace: true });
  }
});
export default router;
